﻿using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;

namespace redis_microservice.Controllers
{
    /// <summary>
    /// 列表 -> 常用于当作队列使用
    /// </summary>
    public class ListController(IConnectionMultiplexer connection) : BaseController(connection)
    {
        /// <summary>
        /// 获取所有项
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> Get(string key = "order:pay")
        {
            var list = await _db0.ListRangeAsync(key);
            return Ok(list.Select(x => new { Item = x.ToString() }));
        }

        /// <summary>
        /// 从左侧新增一项
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> LPush(string key = "order:pay", string val = "666")
        { 
            long len = await _db0.ListLeftPushAsync(key, val);
            return Ok(len);
        }

        /// <summary>
        /// 从右侧取出一项
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> RPop(string key = "order:pay")
        {
            var item = await _db0.ListRightPopAsync(key);
            return Ok(item.ToString());
        }
    }
}
